//
//  JOKETableViewCell.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/5/23.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "JOKETableViewCell.h"
#import "UIImageView+WebCache.h"
#import "tagsModel.h"
@implementation JOKETableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
    
    self.HeadImageV = [[UIImageView alloc] init];
    self.HeadImageV.backgroundColor = [UIColor blueColor];
    //给边框加弧度
    self.HeadImageV.layer.masksToBounds = YES;
    self.HeadImageV.layer.cornerRadius = 15;
    [self.contentView addSubview:self.HeadImageV];
    
    self.NameLabel = [[UILabel alloc] init];
    self.NameLabel.backgroundColor = [UIColor clearColor];
    self.NameLabel.font = [UIFont systemFontOfSize:13];
    self.NameLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.NameLabel];
    
    self.TimeLabel = [[UILabel alloc] init];
    self.TimeLabel.backgroundColor = [UIColor clearColor];
    self.TimeLabel.textColor = DSRGBA(124, 124, 124, 1);
    self.TimeLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.TimeLabel];
    
    self.TitleLabel = [[UILabel alloc] init];
    self.TitleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.TitleLabel];
    // 文字自动换行
    self.TitleLabel.numberOfLines = 0;
    // 自适应高度
    [self.TitleLabel sizeToFit];
    
    self.WorksImageV = [[UIImageView alloc] init];
    self.WorksImageV.backgroundColor = DSRGBA(223, 223, 223, 1);
    [self.contentView addSubview:self.WorksImageV];
    
    self.TypeImageV = [[UIImageView alloc] init];
    self.TypeImageV.image = [UIImage imageNamed:@"tagicon"];
    [self.contentView addSubview:self.TypeImageV];
    
    self.TypeLabel = [[UILabel alloc] init];
    self.TypeLabel.backgroundColor = [UIColor clearColor];
    self.TypeLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.TypeLabel];
    // 点赞图片
    self.upButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.upButton setBackgroundImage:[UIImage imageNamed:@"mainCellDing"] forState:UIControlStateNormal];
    [self.upButton setBackgroundImage:[UIImage imageNamed:@"mainCellDingClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_upButton];
    [self.upButton addTarget:self action:@selector(upButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 不赞图片
    self.downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.downButton setBackgroundImage:[UIImage imageNamed:@"mainCellCai"] forState:UIControlStateNormal];
    [self.downButton setBackgroundImage:[UIImage imageNamed:@"mainCellCaiClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_downButton];
    [self.downButton addTarget:self action:@selector(downButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 发送图片
    self.forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.forwardButton setBackgroundImage:[UIImage imageNamed:@"mainCellShare"] forState:UIControlStateNormal];
    [self.forwardButton setBackgroundImage:[UIImage imageNamed:@"mainCellShareClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_forwardButton];
    [self.forwardButton addTarget:self action:@selector(forwardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 评论
    self.commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.commentButton setBackgroundImage:[UIImage imageNamed:@"mainCellComment"] forState:UIControlStateNormal];
    [self.commentButton setBackgroundImage:[UIImage imageNamed:@"mainCellCommentClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_commentButton];
    [self.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.upLabel = [[UILabel alloc] init];
    self.upLabel.backgroundColor = [UIColor clearColor];
    self.upLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.upLabel];
    
    self.downlabel = [[UILabel alloc] init];
    self.downlabel.backgroundColor = [UIColor clearColor];
    self.downlabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.downlabel];
    
    self.forwardLabel = [[UILabel alloc] init];
    self.forwardLabel.backgroundColor = [UIColor clearColor];
    self.forwardLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.forwardLabel];
    
    self.commentLabel = [[UILabel alloc] init];
    self.commentLabel.backgroundColor = [UIColor clearColor];
    self.commentLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.commentLabel];
    
    self.bigView = [[BaseUIView alloc] init];
//    self.bigView.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.bigView];
    
    self.viewOne = [[BaseUIView alloc] init];
//    self.viewOne.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.viewOne];
    
    self.viewTwo = [[BaseUIView alloc] init];
//    self.viewTwo.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.viewTwo];
    
    self.viewThree = [[BaseUIView alloc] init];
//    self.viewThree.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.viewThree];
    
    self.crossView = [[BaseUIView alloc] init];
//    self.crossView.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.crossView];
    
    
    
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.bigView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 10);
    self.HeadImageV.frame = CGRectMake(10, 20, 30, 30);
    self.NameLabel.frame = CGRectMake(45, 20, 200, 15);
    self.TimeLabel.frame = CGRectMake(45, 35, 200, 15);
    // 计算TitleLabel的高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    CGRect rect = [self.jokeModel.text boundingRectWithSize:CGSizeMake(self.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // TitleLabel的标题(自适应高度)
    self.TitleLabel.frame = CGRectMake(10, 70, self.contentView.frame.size.width - 20, rect.size.height);
    // 图片的自适应高度
    if ([_jokeModel.type isEqualToString:@"gif"]) {
        self.WorksImageV.frame = CGRectMake(10, 80 + rect.size.height, self.contentView.frame.size.width - 20, _jokeModel.gifModel.height.intValue);
        self.TypeImageV.frame = CGRectMake(10, 80 + rect.size.height + _jokeModel.gifModel.height.intValue + 10, 20, 20);
        self.TypeLabel.frame = CGRectMake(40, 90 + rect.size.height + _jokeModel.gifModel.height.intValue, 300, 20);
        self.crossView.frame = CGRectMake(0, 120 + rect.size.height + _jokeModel.gifModel.height.intValue, self.contentView.frame.size.width, 1);
        self.upButton.frame = CGRectMake(20, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 20, 20);
        // --------------------上下各自复印
        self.upLabel.frame = CGRectMake(40 + 7, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 40, 20);
        self.viewOne.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 1, 20);
        self.viewTwo.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 1, 20);
        self.viewThree.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 1, 20);
        self.downButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 20, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 20, 20);
        self.downlabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 40 + 7, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 40, 20);
        self.forwardButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 20, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 20, 20);
        self.forwardLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 40 + 7, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 40, 20);
        self.commentButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 20, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 20, 20);
        self.commentLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 40 + 7, 130 + rect.size.height + _jokeModel.gifModel.height.intValue, 40, 20);
        
        
        
        
    }else if ([_jokeModel.type isEqualToString:@"video"]){
        self.WorksImageV.frame = CGRectMake(10, 80 + rect.size.height, self.contentView.frame.size.width - 20, _jokeModel.videoModel.height.intValue);
        self.TypeImageV.frame = CGRectMake(10, 80 + rect.size.height + _jokeModel.videoModel.height.intValue + 10, 20, 20);
        self.TypeLabel.frame = CGRectMake(40, 90 + rect.size.height + _jokeModel.videoModel.height.intValue, 300, 20);
        self.crossView.frame = CGRectMake(0, 120 + rect.size.height + _jokeModel.videoModel.height.intValue, self.contentView.frame.size.width, 1);
        self.upButton.frame = CGRectMake(20, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 20, 20);
        // --------------------上下各自复印
        self.upLabel.frame = CGRectMake(40 + 7, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 40, 20);
        self.viewOne.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 1, 20);
        self.viewTwo.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 1, 20);
        self.viewThree.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 1, 20);
        self.downButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 20, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 20, 20);
        self.downlabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 40 + 7, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 40, 20);
        self.forwardButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 20, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 20, 20);
        self.forwardLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 40 + 7, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 40, 20);
        self.commentButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 20, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 20, 20);
        self.commentLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 40 + 7, 130 + rect.size.height + _jokeModel.videoModel.height.intValue, 40, 20);
        
        
        
        
    }else {
        self.WorksImageV.frame = CGRectMake(10, 80 + rect.size.height, self.contentView.frame.size.width - 20, _jokeModel.imageModel.height.intValue);
        self.TypeImageV.frame = CGRectMake(10, 80 + rect.size.height + _jokeModel.imageModel.height.intValue + 10, 20, 20);
        self.TypeLabel.frame = CGRectMake(40, 90 + rect.size.height + _jokeModel.imageModel.height.intValue, 300, 20);
        self.crossView.frame = CGRectMake(0, 120 + rect.size.height + _jokeModel.imageModel.height.intValue, self.contentView.frame.size.width, 1);
        self.upButton.frame = CGRectMake(20, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 20, 20);
        // --------------------上下各自复印
        self.upLabel.frame = CGRectMake(40 + 7, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 40, 20);
        self.viewOne.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 1, 20);
        self.viewTwo.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 1, 20);
        self.viewThree.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 1, 20);
        self.downButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 20, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 20, 20);
        self.downlabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 40 + 7, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 40, 20);
        self.forwardButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 20, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 20, 20);
        self.forwardLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 40 + 7, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 40, 20);
        self.commentButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 20, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 20, 20);
        self.commentLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 40 + 7, 130 + rect.size.height + _jokeModel.imageModel.height.intValue, 40, 20);
        
    }
    
    
    
    
    
}

-(void)setJokeModel:(JOKEModel *)jokeModel{
    NSMutableArray *nameArray = [NSMutableArray array];
    _jokeModel = jokeModel;
    
    self.NameLabel.text = _jokeModel.umodel.name;
    self.TimeLabel.text = _jokeModel.passtime;
    for (tagsModel *tagsM in _jokeModel.tags) {
        [nameArray addObject:tagsM.name];
    }
    
    
    NSString *str = [NSString stringWithFormat:@"%@",nameArray.lastObject];
    for (int i = 1; i < nameArray.count; i++) {
        str = [NSString stringWithFormat:@"%@  %@", str, nameArray[i]];
    }
    self.TypeLabel.text = str;
    
    
    
    
    self.upLabel.text = _jokeModel.up;
    NSString *down = [NSString stringWithFormat:@"%@",_jokeModel.down];
    self.downlabel.text = down;
    self.forwardLabel.text = [NSString stringWithFormat:@"%@", _jokeModel.forward];
    self.commentLabel.text = [NSString stringWithFormat:@"%@",_jokeModel.comment];
    
    [self.HeadImageV sd_setImageWithURL:[NSURL URLWithString:_jokeModel.umodel.header[0]] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
    
    self.TitleLabel.text = jokeModel.text;
    
    if ([_jokeModel.type isEqualToString:@"gif"]) {
        
        [self.WorksImageV sd_setImageWithURL:[NSURL URLWithString:(_jokeModel.gifModel.images[0])] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
    }else if ([_jokeModel.type isEqualToString:@"video"]) {
        
        [self.WorksImageV sd_setImageWithURL:[NSURL URLWithString:(_jokeModel.videoModel.thumbnail[0])] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
    }else{
        
        [self.WorksImageV sd_setImageWithURL:[NSURL URLWithString:(_jokeModel.imageModel.big[0])] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
        
    }

}

-(void)upButtonAction:(UIButton *)button{
    DSLog(@"我是精华段子页(点赞按钮)点击方法");
    
}
-(void)downButtonAction:(UIButton *)button{
    DSLog(@"我是精华段子页(不赞按钮)点击方法");
    
}
-(void)forwardButtonAction:(UIButton *)button{
    DSLog(@"我是精华段子页(发送按钮)点击方法");
    
}
-(void)commentButtonAction:(UIButton *)button{
    DSLog(@"我是精华段子页(评论按钮)点击方法");
    
}

@end
